import pygame

class Ship():

    def __init__(self, ai_setting, screen):
        """初始化飞船"""
        self.screen = screen
        self.ai_setting = ai_setting

        #加载飞船图片并获取其外接矩形
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        #将每艘飞船放在屏幕中央底部
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        #
        self.x = float(self.rect.centerx)
        self.y = float(self.rect.centery)
        #
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        """更新左右位置"""
        """"
           print('--------------------------')
            print(self.rect.right)
            print(self.screen_rect.right)
            print(self.rect.left)
            print(self.screen_rect.left)
            print(self.rect.top)
            print(self.screen_rect.top)
            print(self.rect.bottom)
            print(self.screen_rect.bottom)
            print('--------------------------')
        """
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.ai_setting.ship_speed_factor
        if self.moving_left and self.rect.left > self.screen_rect.left:
            self.x -= self.ai_setting.ship_speed_factor
        if self.moving_up and self.rect.top > self.screen_rect.top:
            self.y -= self.ai_setting.ship_speed_factor
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.y += self.ai_setting.ship_speed_factor

        #根据自身更新rect对象
        self.rect.centerx = self.x
        self.rect.centery = self.y

    def blitme(self):
        """在指定位置绘制飞船"""
        self.screen.blit(self.image,self.rect)

    def center_ship(self):
        """初始化飞船位置"""
        self.center = self.screen_rect.centerx